package org.wattdepot.cli;

import java.util.List;
import org.wattdepot.client.BadXmlException;
import org.wattdepot.client.MiscClientException;
import org.wattdepot.client.NotAuthorizedException;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.client.WattDepotClientException;
import org.wattdepot.resource.source.jaxb.Source;
import org.wattdepot.resource.source.summary.jaxb.SourceSummary;
import org.wattdepot.resource.property.jaxb.Property;

/**
 * Prints a list summarizing a specific power plant requested by the user.
 * @author Edwin Lim, Wing Cheung Ma
 * 
 */
public class ListSrcSummary extends CommandLineInterface implements WattDepotCliCommand {
  
  /**
   * Calls the method that would print the summarized list of a single power plant.
   * @param args Strings of input arguments
   * @return output a collection of information about one source
   */
  public String doCommand(List<String> args) {
    String input = args.get(0);
    String output = "";
    Source source = null;
    SourceSummary sourceSummary = null;
    String early = "";
    String latest = "";
    long total = 0;
    
    try {
      source = searchSource(input);
    }
    catch (WattDepotClientException eCli) {
      eCli.printStackTrace();
    }
    StringBuffer sb = new StringBuffer(255);
    String subOutput = "";
    
    //check in for null pointer cases
    if (source != null) {
    
      // check if any subsource is available
      if (source.isSetSubSources()) {
        for (String subsource : source.getSubSources().getHref()) {
        // finds the substring that starts with SIM
          int i = subsource.indexOf("SIM");
          sb.append(subsource.substring(i)).append(", ");
        }    
        subOutput = sb.toString();
      }   
      else {
        System.out.println("SubSources: none");
      }

    // print the owner
      String owner = source.getOwner();
      int j = owner.indexOf("users");
    
    // print properties
      StringBuffer prop = new StringBuffer(255);
      String properties = "";
      if (source.isSetProperties()) {
        for (Property property : source.getProperties().getProperty()) {
          prop.append('[');
          prop.append(property.getKey());
          prop.append(", ");
          prop.append(property.getValue());
          prop.append("] ");
        }
      }
      properties = prop.toString();
    
    
      // print plant data
      String data = source.getName();
      try {
        sourceSummary = client.getSourceSummary(data);
      }
      catch (NotAuthorizedException e) {
      // TODO Auto-generated catch block
        e.printStackTrace();
      }
      catch (ResourceNotFoundException e) {
      // TODO Auto-generated catch block
        e.printStackTrace();
      }
      catch (BadXmlException e) {
      // TODO Auto-generated catch block
        e.printStackTrace();
      }
      catch (MiscClientException e) {
      // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
    if (sourceSummary != null) {
      early = sourceSummary.getFirstSensorData().toString();
      latest = sourceSummary.getLastSensorData().toString();
      total = sourceSummary.getTotalSensorDatas();
    }
    // find and prints the summarized information
    output = "Description: " + source.getDescription() + "\n" +
        "Owner: " + owner.substring(j + 6) + "\n" +
        "Location: " + source.getLocation() + "\n" +
        "Coordinates: " + source.getCoordinates() + "\n" +
        "Properties: " + properties  + "\n" +
        "Earliest Data: " + early + "\n" +
        "Latest Data: " + latest + "\n" +
        "Total Data: " + total;
    }
    output = subOutput + output;
    return output;

  }

}
